<?php
/*
 * СПВ
 **/
class SP_Form_Select extends SP_Form_CommonField
{
	protected $options=array();
	protected $multiple=false;
	protected $size='1';
	protected $default=array();
	
	public function getHtml()
	{
		$vars=$this->getForHtml();
		extract($vars);
		$multiple = ($this->multiple)? ' multiple' : '';
		$name = ($this->multiple)? $name.'[]' : $name ;
		$out='<select name="'.$name.'" id="'.$id.'" size="'.$this->size.'"'.$attr.' '.$multiple.$disabled.'>';
		$values=(array)$value;
		foreach($this->options as $value => $title)
		{
			$selected = in_array($value, $values)? ' selected' : ''; 
			$out.='<option'.$selected.' value="'.$value.'">'.$title.'</option>';
		}
		return $out.'</select>';
	}

	public function setValue($value)
	{
		
		$this->value=(array)$value;
		//var_dump($this->value);
	}
	
	public function getDefault()
	{
		if( $this->default==':first' )
		{
			foreach ($this->options as $key => $value)
			{
				$this->default=$key;
				break;
			} 
		}
		return $this->default;
	}
	
	public function getValue()
	{
		if( !$this->multiple ) return $this->value[0];
		return $this->value;
	}
	
}